//
//  BasicAlertManager.m
//  CultureTrade
//
//  Created by SZFT1 on 15/12/9.
//  Copyright (c) 2015年 cheng.zongxin. All rights reserved.
//

#import "BasicAlertManager.h"
#import "BasicAlertView.h"

#define AlertViewWidth SCREEN_WITDTH - 70 //350.0f
#define AlertViewHeight 194.f
#define kTitleFont      [UIFont boldSystemFontOfSize:16]
#define kSubtitleFont   [UIFont systemFontOfSize:12]
#define kMessageFont    [UIFont boldSystemFontOfSize:12]
#define kButtonFont     [UIFont systemFontOfSize:17]

@interface BasicAlertManager ()
{
    BasicAlertView *_passInAlert;
}
@end


@implementation BasicAlertManager

singleton_implementation(BasicAlertManager)

/*
- (instancetype)init
{
    self =  [super init];
    return self;
}
*/
- (void)show:(id)entity
{
    _passInAlert = entity;
    [self prepareAlertToDisplay:entity];
    [self showAlertViewWithAnimation:entity];
}

- (void)prepareAlertToDisplay:(BasicAlertView *)entity
{
    CGRect screenBounds = [UIScreen mainScreen].bounds;
    if(!_blackBG)
    {
        _blackBG = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenBounds.size.width, screenBounds.size.height)];
        _blackBG.backgroundColor = [UIColor blackColor];
        _blackBG.opaque = YES;
        _blackBG.alpha = 0.1f;
        _blackBG.userInteractionEnabled = YES;
    }
    
    _alertView = nil;
    
    UIImage *bg = [UIImage imageNamed:@"alert_bg"];
//    _alertView = [[UIImageView alloc] initWithImage:[bg stretchableImageWithLeftCapWidth:bg.size.width/2 topCapHeight:bg.size.height/2]];
    _alertView = [[UIImageView alloc] initWithImage:bg];
    _alertView.userInteractionEnabled = YES;
    _alertView.frame = CGRectMake(0, 0, AlertViewWidth, AlertViewHeight);
    _alertView.center = CGPointMake(screenBounds.size.width / 2, screenBounds.size.height / 2);
    
    UILabel *_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, AlertViewWidth, 50)];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.textColor = [UIColor blackColor];
    _titleLabel.font = kTitleFont;
    _titleLabel.text = entity.title;
    [_alertView addSubview:_titleLabel];
    
    UILabel *_subtitleLable = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, AlertViewWidth, 20)];
    _subtitleLable.textAlignment = NSTextAlignmentLeft;
    _subtitleLable.backgroundColor = [UIColor clearColor];
    _subtitleLable.textColor = [UIColor blackColor];
    _subtitleLable.font = kSubtitleFont;
    _subtitleLable.text = entity.subtitle;
    [_alertView addSubview:_subtitleLable];
    
    UILabel *_messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 80, AlertViewWidth, 50)];
    _messageLabel.textAlignment = NSTextAlignmentLeft;
    _messageLabel.backgroundColor = [UIColor clearColor];
    _messageLabel.textColor = [UIColor whiteColor];
    _messageLabel.font = kMessageFont;
    _messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
    _messageLabel.numberOfLines = 3;
    _messageLabel.text = entity.message;
    if(_messageLabel.text == nil) _messageLabel.text = @"";
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:_messageLabel.text];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    
    [paragraphStyle setLineSpacing:5];
    
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, _messageLabel.text.length)];
    
    
    _messageLabel.attributedText = attributedString;
    
    //调节高度
    CGRect messageFrame = _messageLabel.frame;
    CGSize labelSize = [_messageLabel sizeThatFits:messageFrame.size];
    messageFrame.size = labelSize;
    _messageLabel.frame = messageFrame;
    [_alertView addSubview:_messageLabel];
    
//    float buttonWidth = (AlertViewWidth - 100.0f) / entity.buttons.count;
//    float buttonPadding = 100.0f / (entity.buttons.count - 1 + 2 * 2);
    
    UIView *sepliteLine = [[UIView alloc] initWithFrame:CGRectMake(0, _alertView.frame.size.height - 60, _alertView.frame.size.width, 1)];
    sepliteLine.backgroundColor = UICOLOR_RGB_Alpha(0xe5e5e5, 1);
    [_alertView addSubview:sepliteLine];
    
    for(int i = 0; i < entity.buttons.count; i++)
    {
        NSString *buttonIcon = [entity.buttons objectAtIndex:i];
        int style_ = [[entity.buttonsStyle objectAtIndex:i] intValue];
        XYButtonStyle style;
        if (style_ == 0) {
            style = XYButtonStyleDefault;
        }else if (style_ == 1){
            style = XYButtonStyleGray;
        }else{
            style = XYButtonStyleGreen;
        }
        
        UIButton *_button = [UIButton buttonWithType:UIButtonTypeSystem];
        [_button setTitle:buttonIcon forState:UIControlStateNormal];
        _button.titleLabel.font = kButtonFont;
//        _button.titleLabel.shadowOffset = CGSizeMake(1, 1);
        [_button setTitleColor:COLOR_MAIN_GOLD forState:UIControlStateNormal];
//        [_button setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
//        [_button setBackgroundImage:[self buttonImageByStyle:style state:UIControlStateNormal]
//                           forState:UIControlStateNormal];
//        [_button setBackgroundImage:[self buttonImageByStyle:style state:UIControlStateHighlighted]
//                           forState:UIControlStateHighlighted];
       //ICON BUTTON
//        UIImage *btnImg = [UIImage imageNamed:buttonIcon];
//        [_button setImage:btnImg forState:UIControlStateNormal];
//        CGFloat x = (AlertViewWidth - btnImg.size.width * 2)/3;
//        _button.frame = CGRectMake(x + i * (btnImg.size.width + x), 150,btnImg.size.width, btnImg.size.height);
        _button.tag = i;
        
        [_button addTarget:self action:@selector(onButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        
//        if (entity.buttons.count == 1) {
//            CGFloat x = (AlertViewWidth - btnImg.size.width)/2;
//            CGFloat y = _button.frame.origin.y;
//            _button.frame = (CGRect){{x,y},btnImg.size};
//        }
        [_alertView addSubview:_button];
        if (entity.buttons.count == 1){
            _button.frame = CGRectMake(0, _alertView.height-40, _alertView.width/entity.buttons.count, 20);
//            [_button mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.bottom.equalTo(_alertView).offset(-20);
//                make.size.mas_equalTo(CGSizeMake(50, 20));
//                make.centerX.mas_equalTo(_alertView.centerX);
//            }];
        }
    }
}

-(void)onButtonTapped:(UIButton *)sender
{
//    [self dismiss:[_alertViewQueue lastObject] button:sender.tag];
    if (_passInAlert.blockAfterDismiss != nil) {
        _passInAlert.blockAfterDismiss((int)sender.tag);
    }
    
    [_alertView removeFromSuperview];
    [_blackBG removeFromSuperview];
    
}

-(void)showAlertViewWithAnimation:(id)entity
{
    if([entity isKindOfClass:[BasicAlertView class]])
    {
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        if(!keyWindow)
        {
            NSArray *windows = [UIApplication sharedApplication].windows;
            if(windows.count > 0) keyWindow = [windows lastObject];
            //            keyWindow = [windows objectAtIndex:0];
        }
        
        _blackBG.alpha = 0.0f;
        CGRect frame = _alertView.frame;
        frame.origin.y = -AlertViewHeight;
        _alertView.frame = frame;
        [keyWindow addSubview:_blackBG];
        [keyWindow addSubview:_alertView];
        [UIView animateWithDuration:0.2f animations:^{
            _blackBG.alpha = 0.5f;
            _alertView.center = CGPointMake([UIScreen mainScreen].applicationFrame.size.width / 2, [UIScreen mainScreen].applicationFrame.size.height / 2);
        }
                         completion:^(BOOL finished) {
                             
                         }];
    }
}


@end
